package com.stm.bi.report;

import com.stm.bi.model.Chart;
import com.stm.bi.model.ChartQuery;
import com.stm.framework.core.context.UserHolder;
import com.stm.framework.dbcp.JdbcService;
import com.stm.framework.dto.Response;
import com.stm.framework.enums.Status;
import com.stm.framework.model.LoginUser;
import com.stm.log.OperLogProvider;
import com.stm.log.enums.ActionType;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author chenyingjun
 * @Desc
 * @Date 2024/3/27 10:57
 */
public abstract class AbsChartQueryExe<Q extends ChartQuery, R extends Chart>{
   protected static final Logger log = LoggerFactory.getLogger(AbsChartQueryExe.class);

   @Resource
   protected OperLogProvider operLogProvider;

   @Resource
   protected JdbcService jdbcService;

    public Response<R> query(Q query){
        Long startTime = System.currentTimeMillis();
        LoginUser loginUser = UserHolder.getLoginUser();
        R queryResult = query(loginUser.getOrgId(),loginUser.getOrgNo(),query);

        //没数据的情况下置空
        if(queryResult.getItems().size() <= 1){
            queryResult.setDatas(null);
        }

        Response response = Response.of(queryResult);
        operLogProvider.create(ActionType.QUERY,"查询报表["+getReportName()+"]",query,null,response,Status.NORMAL);
        log.info("查询报表["+getReportName()+"]耗时："+(System.currentTimeMillis()-startTime)+"ms");
        return response;
    }

    public abstract R query(Long orgId,String orgNo,Q query);

    public abstract String getReportName();

}
